﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace HDJ.Framework.Core.Editor
{
    /// <summary>
    /// 绘制关联表格
    /// </summary>
    public class TableDataDrawTableField : TableDataDrawFieldBase
    {
        public override Type FieldType => typeof(string);

        public override string Tag => "TableValue";

        List<string> configFileNames = new List<string>();
        public override void Init()
        {
            configFileNames.AddRange(PathUtils.GetDirectoryFileNames(TableDefine.SaveConfigFilePath, new string[] { ".txt" }, false, false));
        }
        private List<string> fieldNames = new List<string>();
        private List<string> tableKeys = new List<string>();

        public override object DrawGUI(string text, object defultValue, TableFieldInfo info)
        {
            string value = defultValue.ToString();
            GUILayout.Label(text + " : " + value);

            string tableName = "";
            string fieldName = "";
            string tableKey = "";
            if (string.IsNullOrEmpty(tableName) && !string.IsNullOrEmpty(value))
            {
                string[] tempStrs = value.Split('/');
                if (tempStrs.Length == 3)
                {
                    tableName = tempStrs[0];
                    fieldName = tempStrs[1];
                    tableKey = tempStrs[2];
                }
            }
            tableName = EditorDrawGUIUtil.DrawPopup("表格名", tableName, configFileNames, (tName) =>
            {
                if (string.IsNullOrEmpty(tName))
                    return;
                fieldNames.Clear();
                tableKeys.Clear();
                TableData d = TableManager.GetTableData(tName);
                fieldNames.AddRange(d.fieldInfos.Keys);
                tableKeys.AddRange(d.datas.Keys);
            });

            fieldName = EditorDrawGUIUtil.DrawPopup("字段名", fieldName, fieldNames);
            tableKey = EditorDrawGUIUtil.DrawPopup("表格数据key", tableKey, tableKeys);

            defultValue = tableName + "/" + fieldName + "/" + tableKey;
            return defultValue;
        }
    }
}

